<?php

    // TITLE: auction
    // FILE: auction.php
    // AUTHOR: dantira
    // CREATED: Mar 21, 2015, 4:43:26 PM

    $createAuction = function($auctionId, $eventId, $date) {
	    global $con;
	    $sql = 'INSERT INTO auction (auction_id, event_id, date) VALUES (' . $auctionId . ', ' . $eventId . ', ' . $date . ')';
	    $this->open();
	    $result = mysqli_query($con, $sql);
	    $id = ($result) ? mysqli_insert_id($con) : $result;
	    $this->close();
	    return $id;
    };// end function

    $readAuction = function($id) {
	    global $con;
	    $sql = 'SELECT * FROM auction WHERE id = ' . $id;
	    $this->open();
	    $results = mysqli_query($con, $sql);
	    $this->close();
	    if ($results) {
		    $result = mysqli_fetch_array($results);
		    $auction = new Auction();
		    $auction->setId($result[0]);
		    $auction->setEventId($this->readEvent($result[1]));
		    $auction->setDate($result[2]);
	    } else {
		    $auction = false;
	    }
	    return $auction;
    };// end function

    $updateAuction = function($auctionId, $eventId, $date) {
	    global $con;
	    $sql = 'UPDATE auction SET auction_id = ' . $auction_id . ', event_id = ' . $eventId . ', date = ' . $date . ' WHERE id = ' . $id;
	    $this->open();
	    $result = mysqli_query($con, $sql);
	    $this->close();
	    return $result;
    };// end function

    $deleteAuction = function($id) {
	    global $con;
	    $sql = 'DELETE FROM auction WHERE auction_id = ' . $id;
	    $this->open();
	    $result = mysqli_query($con, $sql);
	    $this->close();
	    return $result;
    };// end function

    $listAuction = function() {
	    global $con;
	    $sql = 'SELECT * FROM auction';
	    $this->open();
	    $results = mysqli_query($con, $sql);
	    $this->close();
	    if ($results) {
		    $auctions = array();
		    while ($result = mysqli_fetch_array($results)) {
			    $auction = new Auction();
			    $auction->setAuctionId($result[0]);
			    $auction->setEventId($this->readEvent($result[1]));
			    $auction->setDate($result[2]);
			    $auctions[] = $auction;
		    };// end while
	    } else {
		    $auctions = false;
	    }
	    return $auctions;
    };// end function

    // auction_item // --------------------

    $createAuctionItem = function($id, $auctionId, $itemNum, $title, $description, $value, $price, $buyerId, $donationId) {
	    global $con;
	    $sql = 'INSERT INTO auction_item (id, auction_id, item_num, title, description, value, price, buyer_id, donation_id) VALUES (' . $id . ', ' . $auctionId . ', ' . $itemNum . ', ' . $title . ', ' . $description . ', ' . $value . ', ' . $price . ', ' . $buyerId . ', ' . $donationId . ')';
	    $this->open();
	    $result = mysqli_query($con, $sql);
	    $id = ($result) ? mysqli_insert_id($con) : $result;
	    $this->close();
	    return $id;
    };// end function

    $readAuctionItem = function($id) {
	    global $con;
	    $sql = 'SELECT * FROM auction_item WHERE id = ' . $id;
	    $this->open();
	    $results = mysqli_query($con, $sql);
	    $this->close();
	    if ($results) {
		    $result = mysqli_fetch_array($results);
		    $auctionItem = new AuctionItem();
		    $auctionItem->setId($result[0]);
		    $auctionItem->setAuctionId($result[1]);
		    $auctionItem->setItemNum($result[2]);
		    $auctionItem->setTitle($result[3]);
		    $auctionItem->setDescription($result[4]);
		    $auctionItem->setValue($result[5]);
		    $auctionItem->setPrice($result[6]);
		    $auctionItem->setBuyerId($this->readBuyer($result[7]));
		    $auctionItem->setDonationId($this->readDonation($result[8]));
	    } else {
		    $auctionItem = false;
	    }
	    return $auctionItem;
    };// end function

    $updateAuctionItem = function($id, $auctionId, $itemNum, $title, $description, $value, $price, $buyerId, $donationId) {
	    global $con;
	    $sql = 'UPDATE auction_item SET auction_id = ' . $auction_id . ', item_num = ' . $itemNum . ', title = ' . $title . ', description = ' . $description . ', value = ' . $value . ', price = ' . $price . ', buyer_id = ' . $buyerId . ', donation_id = ' . $donationId . ' WHERE id = ' . $id;
	    $this->open();
	    $result = mysqli_query($con, $sql);
	    $this->close();
	    return $result;
    };// end function

    $deleteAuctionItem = function($id) {
	    global $con;
	    $sql = 'DELETE FROM auction_item WHERE id = ' . $id;
	    $this->open();
	    $result = mysqli_query($con, $sql);
	    $this->close();
	    return $result;
    };// end function

    $listAuctionItem = function() {
	    global $con;
	    $sql = 'SELECT * FROM auction_item';
	    $this->open();
	    $results = mysqli_query($con, $sql);
	    $this->close();
	    if ($results) {
		    $auctionItems = array();
		    while ($result = mysqli_fetch_array($results)) {
			    $auctionItem = new AuctionItem();
			    $auctionItem->setId($result[0]);
			    $auctionItem->setAuctionId($result[1]);
			    $auctionItem->setItemNum($result[2]);
			    $auctionItem->setTitle($result[3]);
			    $auctionItem->setDescription($result[4]);
			    $auctionItem->setValue($result[5]);
			    $auctionItem->setPrice($result[6]);
			    $auctionItem->setBuyerId($this->readBuyer($result[7]));
			    $auctionItem->setDonationId($this->readDonation($result[8]));
			    $auctionItems[] = $auctionItem;
		    };// end while
	    } else {
		    $auctionItems = false;
	    }
	    return $auctionItems;
    };// end function
